package de.recipeminer.ui.commands;

import joptsimple.OptionSet;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * @autor: Markus Ackermann
 */
public class CommandBuilder {
    private static Logger logger = Logger.getLogger(CommandBuilder.class);   
    private OptionSet current_options;
    private String current_commandName;
    private static Map<String, Class<? extends UserCommand>> supportedCommands = new HashMap<String,Class<? extends UserCommand>>();

    public static boolean canBuild(String commandName) {
        return supportedCommands.containsKey(commandName);
    }

    /*public static Map<String, Class<? extends UserCommand>> supportedCommands() {
        if (supportedCommands.isEmpty()) {
            //listing commands
            supportedCommands.put("listraw",ListRawRecipesCommand.class);
            supportedCommands.put("writestopwords",ListStopwordsCommand.class);
            supportedCommands.put("show",ShowRecipeCommand.class);
            supportedCommands.put("termlist",ListTermListCommand.class);
            supportedCommands.put("showsimilar", ShowRecipeComparison.class);

            //workflow commands
            supportedCommands.put("crawl",CrawlingCommand.class);
            supportedCommands.put("extract",ExtractionCommand.class);
            supportedCommands.put("readstopwords",ReadStopWordsCommand.class);
            supportedCommands.put("updateterms", UpdateTermStatisticsCommand.class);
        }
        return supportedCommands;
    }*/
    

    public CommandBuilder(OptionSet options) throws UnsupportedOperationException  {
        this.current_options = options;

    }

    public UserCommand build(String commandName) throws UnsupportedOperationException {


        if (canBuild(commandName)) {
            this.current_commandName = commandName;
            Class<? extends UserCommand> commandClass = supportedCommands.get(commandName);
            UserCommand builtCommand;
            //access to constructor
            try {
                 builtCommand = commandClass.newInstance();
            }
            catch (Exception e) {
                throw new UnsupportedOperationException("Command not found.");  
            }

            return builtCommand;  }
        else {
        throw new UnsupportedOperationException("Command not found.");
        }
    }

 


  
}
